package com.controller;

import com.google.gson.Gson;
import entry.Book;
import pojo.BookMess;
import service.BookService;
import service.Impl.BookServiceImpl;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@SuppressWarnings("ALL")
@WebServlet("/book/*")
public class BookServlet extends HttpServlet {
    private BookService bookService = new BookServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = BookServlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    //查询全部数据
   /* public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageHelper pageHelper=new PageHelper();
        pageHelper.setCountData(bookService.selectBookSize(new Book()));
        pageHelper.setList(bookService.getBooks());
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(pageHelper));
    }*/

    //按条件查询数据
    public void queryBybook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String query = request.getParameter("query");
        String str = request.getParameter("str");
        String index = request.getParameter("pageindex");
        Book book = new Book();
        if ("1".equals(query)) {
            book.setBookName(str);
        } else if ("2".equals(query)) {
            book.setBookAuthor(str);
        } else if ("3".equals(query)) {
            book.setBookPublish(str);
        }
        PageHelper ph = new PageHelper();
        if (index != null) {
            ph.setIndexPage(Integer.parseInt(index));
        }
        ArrayList<Book> books = bookService.selectBook(book, ph);
        System.out.println(books);
        Gson gson = new Gson();
        ph.setList(books);
        ph.setCountData(bookService.selectBookSize(book));
        response.getWriter().println(gson.toJson(ph));
        System.out.println(gson.toJson(ph));
    }

    public void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        String bookName = request.getParameter("BookName");
        String bookAuthor = request.getParameter("BookAuthor");
        String val = request.getParameter("val");
        String val1 = request.getParameter("val1");
        String val2 = request.getParameter("val2");
        String val3 = request.getParameter("val3");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Book boo = new Book();
        boo.setBookName(bookName);
        boo.setBookAuthor(bookAuthor);
        boo.setBookPublish(val);
        boo.setBookPage(Integer.parseInt(val1));
        boo.setBookPrice(Integer.parseInt(val2));
        boo.setCreateDate(val3);
        boolean b = bookService.insertBook(boo);
        response.getWriter().println(b);

    }

    public void deleBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        String bookId = request.getParameter("bookId");
        System.out.println(bookId);
        Book book = new Book();
        book.setBookId(Integer.parseInt(bookId));
        System.out.println(book);
        boolean b = bookService.deleteBook(book);
        response.getWriter().println(b);
    }

    public void upBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        String bookId = request.getParameter("bookId");
        String bookName = request.getParameter("BookName");
        String bookAuthor = request.getParameter("BookAuthor");
        String val = request.getParameter("val");
        String val1 = request.getParameter("val1");
        String val2 = request.getParameter("val2");
        String val3 = request.getParameter("val3");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Book boo = new Book();
        boo.setBookId(Integer.parseInt(bookId));
        boo.setBookName(bookName);
        boo.setBookAuthor(bookAuthor);
        boo.setBookPublish(val);
        boo.setBookPage(Integer.parseInt(val1));
        boo.setBookPrice(Integer.parseInt(val2));
        boo.setCreateDate(val3);
        boolean b = bookService.updateBook(boo);
        response.getWriter().println(b);
    }

    public void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        BookMess bookMess = new BookMess();
        bookMess.setMess(bookService.deleteBook1(bookId) ? "\"删除成功\"" : "\"删除失败\"");
        //获得PageHelper对象
        String query = request.getParameter("query");
        String str = request.getParameter("str");
        String index = request.getParameter("pageindex");
        Book book = new Book();
        if ("1".equals(query)) {
            book.setBookName(str);
        } else if ("2".equals(query)) {
            book.setBookAuthor(str);
        } else if ("3".equals(query)) {
            book.setBookPublish(str);
        }
        PageHelper ph = new PageHelper();
        if (index != null) {
            ph.setIndexPage(Integer.parseInt(index));
        }
        ArrayList<Book> books = bookService.selectBook(book, ph);
        Gson gson = new Gson();

        ph.setList(books);
        ph.setCountData(bookService.selectBookSize(book));
        response.getWriter().println(gson.toJson(bookMess));

    }

    public void queryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        Book book = bookService.selectBookById(bookId);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(book));


    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int bid = Integer.parseInt(request.getParameter("bid"));
        String bname = request.getParameter("bname");
        String aname = request.getParameter("aname");
        String cname = request.getParameter("cname");
        int page = Integer.parseInt(request.getParameter("page"));
        int price = Integer.parseInt(request.getParameter("price"));
        String date = request.getParameter("date");
        Book boo = new Book();
        boo.setBookId(bid);
        boo.setBookName(bname);
        boo.setBookAuthor(aname);
        boo.setBookPublish(cname);
        boo.setBookPage(page);
        boo.setBookPrice(price);
        boo.setCreateDate(date);
        boolean b = bookService.updateBook(boo);
        response.getWriter().println(b);
    }
}
